<template>
  <div class="demo-throttle">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>防抖演示</span>
            </div>
          </template>
          <el-input 
            v-model="searchText"
            placeholder="输入搜索内容"
            @input="handleDebounceSearch"
          />
          <div class="result">搜索结果: {{ searchResult }}</div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>节流演示</span>
            </div>
          </template>
          <el-button 
            type="primary"
            @click="handleThrottleClick"
          >
            点击触发 ({{ count }})
          </el-button>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 添加一些内容以测试滚动 -->
    <el-row style="margin-top: 20px">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>滚动测试</span>
            </div>
          </template>
          <div style="height: 500px; display: flex; align-items: center; justify-content: center;">
            超出内容区域时会出现滚动条
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { debounce, throttle } from '@/utils/throttleDebounce'

// 防抖相关
const searchText = ref('')
const searchResult = ref('')

// 防抖函数：延迟500ms执行
const handleDebounceSearch = debounce((e: Event) => {
  const target = e.target as HTMLInputElement
  searchResult.value = target.value
}, 500)

// 节流相关
const count = ref(0)

// 节流函数：每1000ms最多执行一次
const handleThrottleClick = throttle(() => {
  count.value++
}, 1000)
</script>

<style scoped lang="scss">
.demo-throttle {
  .result {
    margin-top: 20px;
    color: #409EFF;
  }
  
  .el-button {
    width: 100%;
  }
}
</style> 